import { OnInit, Component, ViewChild, Input } from '@angular/core';
import { ComprehensiveEvaluationDetailComponent } from './comprehensive-evaluation-detail.component';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { ComprehensiveScoreDTO } from '../../../model/icm/icm-student-academic/comprehensive-evaluation/comprehensive-evaluation.dto';
import { ObjectUtil } from 'src/app/project/utils/object-util';
import { TABLE_PARAMETER } from '../../setting/table-parameter';
import { STColumn } from '@delon/abc';
import { NzMessageService } from 'ng-zorro-antd';
import { ComprehensiveScoreService } from '../../../service/icm/icm-student-academic/comprehensive-evaluation/comprehensive-score.service';
import { format } from 'date-fns';
import { StudentDTO } from 'src/app/project/model/icm/icm-system-user/student/student.dto';
import { SettingsService } from '@delon/theme';
import { StudentService } from 'src/app/project/service/icm/icm-system-user/student/student.service';

@Component({
  selector: 'app-comprehensive-evaluation-list',
  templateUrl: './comprehensive-evaluation-list.component.html',
  styles: [],
})
export class ComprehensiveEvaluationListComponent implements OnInit {
  constructor(
    private msg: NzMessageService,
    private comprehensiveScoreService: ComprehensiveScoreService,
    private settingService: SettingsService,
  ) {}

  @ViewChild('comprehensiveEvaluationDetailComponent', { static: false })
  comprehensiveEvaluationDetailComponent: ComprehensiveEvaluationDetailComponent;

  /**
   * 分页参数初始化
   */
  queryOptions: QueryOptions = {
    page: 0,
    size: 20,
    sort: 'year,asc',
  };

  /**
   * 查询参数
   */
  @Input()
  queryParams;

  /**
   * 当前登录教师
   */
  //   teacher: TeacherDTO;
  teacherId = null;

  /**
   * 当前登录学生
   */
  @Input()
  student: StudentDTO;

  /**
   * 请求数据标识
   */
  loading = false;

  /**
   * 表格数据
   */
  tableData: Array<ComprehensiveScoreDTO> = [];

  /**
   * 列表参数
   */
  TABLE_PARAMETER = ObjectUtil.deepClone(TABLE_PARAMETER);

  /**
   * 表格的列定义
   */
  stColumns: STColumn[] = [
    {
      title: '序号',
      render: 'number',
      width: '2%',
      className: 'text-center',
    },
    {
      title: '姓名',
      index: 'studentName',
      width: '2%',
      className: 'text-left',
    },
    {
      title: '学号',
      index: 'studentCode',
      width: '3%',
      className: 'text-left',
    },
    {
      title: '综合测评学年',
      index: 'year',
      width: '4%',
      className: 'text-center',
      format(item) {
        return `${item.year} - ${Number.parseInt(item.year, 10) + 1} 学年`;
      },
    },
    {
      title: '学业分合计',
      render: 'academicTotal',
      width: '2%',
      className: 'text-right',
    },
    {
      title: '思想分合计',
      render: 'ideologicalTotal',
      width: '2%',
      className: 'text-right',
    },
    {
      title: '文体分合计',
      render: 'stylisticTotal',
      width: '2%',
      className: 'text-right',
    },
    {
      title: '综合分合计',
      render: 'comprehensiveScoreTotal',
      width: '2%',
      className: 'text-right',
    },
    {
      title: '流程状态',
      index: 'compAssessProcessStatus',
      width: '2%',
      className: 'text-left',
      format(item) {
        return item.compAssessProcessStatus === 'SELF_ASSESSMENT'
          ? '学生自评'
          : item.compAssessProcessStatus === 'CLASS_ASSESSMENT'
          ? '班级评议'
          : item.compAssessProcessStatus === 'GRADE_REVIEW'
          ? '年级复审'
          : item.compAssessProcessStatus === 'DONE_ASSESSMENT'
          ? '公示3天'
          : item.compAssessProcessStatus === 'DONE_CLOSE'
          ? '审结'
          : '';
      },
    },
    {
      title: '报送状态',
      index: 'isReport',
      width: '2%',
      className: 'text-center',
      format(item) {
        return item.compAssessProcessStatus === 'DONE_CLOSE' ? '已审结' : item.isReport === true ? '已报送' : '未报送';
      },
    },
    {
      title: '操作',
      render: 'operations',
      index: 'operation',
      width: '5%',
      className: 'text-center',
      fixed: 'right',
    },
  ];

  /**
   * 表格变化事件，用于双击、排序，翻页等操作
   * @param e event
   */
  change(e: any): void {
    // 双击事件
    if (e.type === 'dblClick') {
      this.loading = true;
      // tslint:disable-next-line: deprecation
      this.comprehensiveScoreService.findById(e.dblClick.item.id).subscribe(
        data => {
          this.comprehensiveEvaluationDetailComponent.edit(true, data, false);
        },
        null,
        () => {
          this.loading = false;
        },
      );
    }
    // 排序事件
    if (e.type === 'sort') {
      if (e.sort.map) {
        const temp = e.sort.map.sort;
        if (temp) {
          this.queryOptions.sort = temp.replace(/[\.-]/g, ',');
        }
      }
      this.loadData();
    }
    // 翻页设置
    if (e.type === 'ps' || e.type === 'pi') {
      this.queryOptions.page = e.pi - 1;
      this.queryOptions.size = e.ps;
      this.loadData();
    }
  }

  /**
   * 获取学生课程成绩数据
   */
  loadData() {
    this.loading = true;
    this.comprehensiveScoreService
      .search(
        this.queryParams.academyName,
        this.queryParams.professionName,
        this.queryParams.className,
        this.queryParams.classId,
        this.queryParams.studentName,
        this.settingService.user.isStudent === true && this.settingService.user.isClassEvaluationAdmin === false
          ? this.settingService.user.id
          : null,
        this.queryParams.year !== null ? format(this.queryParams.year, 'YYYY') : '',
        this.queryParams.compAssessProcessStatus,
        this.queryParams.studentCode,
      )
      // tslint:disable-next-line: deprecation
      .subscribe(
        data => {
          if (data) {
            this.tableData = data;
            this.TABLE_PARAMETER.page.total = data.length;
          }
        },
        null,
        () => {
          this.loading = false;
        },
      );
  }

  /**
   * 送审综合测评
   * @param row 学生综合测评
   */
  onUpdate(row: ComprehensiveScoreDTO) {
    this.loading = true;
    // tslint:disable-next-line: deprecation
    // this.comprehensiveScoreService.findById(row.id).subscribe(
    //   data => {
    //     // this.comprehensiveEvaluationDetailComponent.edit(false, data);
    //   },
    //   null,
    //   () => {
    //     this.loading = false;
    //   },
    // );
  }

  /**
   * 查看
   */
  view(row: ComprehensiveScoreDTO) {
    // tslint:disable-next-line: deprecation
    this.comprehensiveScoreService.findById(row.id).subscribe(
      data => {
        this.comprehensiveEvaluationDetailComponent.edit(true, data, false);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 修改
   */
  modify(row: ComprehensiveScoreDTO) {
    // tslint:disable-next-line: deprecation
    this.comprehensiveScoreService.findById(row.id).subscribe(
      data => {
        this.comprehensiveEvaluationDetailComponent.edit(false, data, false);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 补录材料
   */
  supplement(row: ComprehensiveScoreDTO) {
    // tslint:disable-next-line: deprecation
    this.comprehensiveScoreService.findById(row.id).subscribe(
      data => {
        this.comprehensiveEvaluationDetailComponent.edit(true, data, true);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  ngOnInit() {
    this.loadData();
  }
}
